﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SlamDunk.MES.Domain.Entities.ConfigurationContext;
using SlamDunk.MES.Domain.IRepositories.Interfaces.ConfigurationContext;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.ConfigurationContext
{
    /// <summary>
    /// 硬件仓储实现
    /// </summary>
    public class HardwareRepository : EFAggRootRepositoryProvider<Hardware>, IHardwareRepository
    {
        #region # 分页获取硬件列表 —— ICollection<Hardware> FindByPage(string keywords...
        /// <summary>
        /// 分页获取硬件列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="hardwareType">硬件类型</param>
        /// <param name="terminalId">终端Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>硬件列表</returns>
        public ICollection<Hardware> FindByPage(string keywords, HardwareType? hardwareType, Guid? terminalId, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<Hardware, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (hardwareType == null || x.HardwareType == hardwareType) &&
                    (terminalId == null || (x.Connection != null && x.Connection.TerminalId == terminalId));

            IQueryable<Hardware> hardwares = base.FindByPage(condition, pageIndex, pageSize, out rowCount, out pageCount);

            return hardwares.ToList();
        }
        #endregion
    }
}
